package me.haoliang


import org.apache.http.HttpResponse
import org.apache.http.client.methods.{HttpGet, HttpPost}
import org.apache.http.entity.{ContentType, StringEntity}
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils

/**
  * author: planck 
  * createTime: 2016/11/11.
  */
object HttpTest {

  var httpClient = HttpClients.createDefault

  def exe(post: HttpPost): HttpResponse = {
    post.setHeader("Content-Type", "application/json;charset=UTF-8")
    post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64)" +
      " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 " +
      "Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat")
    post.setHeader("X-Requested-With", "XMLHttpRequest")
    post.setHeader("Referer", "http://wx.hduhelp.com/app/index.php?i=2&c=entry&id=2&do=ShowVote1&m=vote")
    post.setHeader("Accept-Encoding", "gzip, deflate")
    post.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en-us;q=0.6,en;q=0.5;q=0.4")
    post.setHeader("Accept", "application/json, text/plain, */*")
    post.setHeader("Host", "wx.hduhelp.com")
    httpClient execute (post)
  }


  def exe(get: HttpGet): HttpResponse = {
    get.setHeader("Content-Type", "application/json;charset=UTF-8")
    get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64)" +
      " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 " +
      "Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat")
    get.setHeader("X-Requested-With", "XMLHttpRequest")
    get.setHeader("Referer", "http://wx.hduhelp.com/app/index.php?i=2&c=entry&id=2&do=ShowVote1&m=vote")
    get.setHeader("Accept-Encoding", "gzip, deflate")
    get.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en-us;q=0.6,en;q=0.5;q=0.4")
    get.setHeader("Accept", "application/json, text/plain, */*")
    get.setHeader("Host", "wx.hduhelp.com")
    httpClient execute (get)
  }

  def exeVote(post: HttpPost): HttpResponse = {
    post.setHeader("Content-Type", "application/json;charset=UTF-8")
    post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64)" +
      " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 " +
      "Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat")
    post.setHeader("X-Requested-With", "XMLHttpRequest")
    post.setHeader("Referer", "http://wx.hduhelp.com/app/index.php?i=2&c=entry&id=2&do=ShowVote1&m=vote")
    post.setHeader("Accept-Encoding", "gzip, deflate")
    post.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en-us;q=0.6,en;q=0.5;q=0.4")
    post.setHeader("Accept", "application/json, text/plain, */*")
    post.setHeader("Host", "wx.hduhelp.com")
    //post.setHeader("Cookie", "PHPSESSID=195d34ff1ea54455b311d852601dfaf3")
    post.setEntity(new StringEntity("{\"newHistory\": [\"44\"]}", ContentType.APPLICATION_JSON))
    httpClient execute (post)
  }

  def main(args: Array[String]): Unit = {

    val prePost = new HttpPost("http://wx.hduhelp.com/app/index.php?i=2&c=entry&id=2&do=ShowVote1&m=vote")
    exe(prePost)
    println(1)
    val prePost1 = new HttpGet("http://wx.hduhelp.com/app/index.php?i=2&c=auth&a=oauth&scope=snsapi_base&code=041dTH4D13Csh109pZ6D1KcB4D1dTH4h&state=we7sid-66d7aaeb606e79ac104c5b935cf373fa")

    exe(prePost1)
    println(2)
    val proPost2 = new HttpGet("http://wx.hduhelp.com/app/index.php?i=2&c=auth&a=oauth&scope=snsapi_base&code=011wDQ982q24fO0qLTb82QCO982wDQ9V&state=we7sid-66d7aaeb606e79ac104c5b935cf373fa")

    exe(proPost2)
    println(3)
    println("start")
    httpClient = HttpClients.createDefault()
    val post = new HttpPost("http://wx.hduhelp.com/app/index.php?i=2&c=entry&id=2&do=DoVote&m=vote")
    println(EntityUtils.toString(exeVote(post).getEntity))

  }


}
